package wu.service;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import wu.pojo.NiuStudent;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelGenerator {
    public static boolean generateExcel(List<NiuStudent> studentList, String filePath,int num) {
        boolean b=false;
        // 创建一个新的Excel工作簿（用于.xlsx格式）
        Workbook workbook = new XSSFWorkbook();
        // 创建一个工作表
        Sheet sheet = workbook.createSheet("研发中心牛客周赛 Round "+num+" 成绩表");

        // 创建表头行
        String[] headers = {"排名", "班级", "姓名", "牛客名称", "牛客id", "名次", "学校", "AC数", "总分", "时间", "A_score", "B_score", "C_score", "D_score", "E_score", "F_score", "G_score"};
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据行
        int rowNum = 1;
        for (NiuStudent student : studentList) {
            Row dataRow = sheet.createRow(rowNum++);
            // 排名（这里假设List的顺序就是排名顺序，可根据实际情况调整）
            dataRow.createCell(0).setCellValue(rowNum - 1);
            dataRow.createCell(1).setCellValue(student.getClassInfo());
            dataRow.createCell(2).setCellValue(student.getName());
            dataRow.createCell(3).setCellValue(student.getNiu_Contestants());
            dataRow.createCell(4).setCellValue(student.getNiu_id());
            dataRow.createCell(5).setCellValue(student.getNiu_Ranking());
            dataRow.createCell(6).setCellValue(student.getNiu_School());
            dataRow.createCell(7).setCellValue(student.getNiu_AC());
            dataRow.createCell(8).setCellValue(student.getNiu_total_score());
            dataRow.createCell(9).setCellValue(student.getNiu_time());
            dataRow.createCell(10).setCellValue(student.getA_score());
            dataRow.createCell(11).setCellValue(student.getB_score());
            dataRow.createCell(12).setCellValue(student.getC_score());
            dataRow.createCell(13).setCellValue(student.getD_score());
            dataRow.createCell(14).setCellValue(student.getE_score());
            dataRow.createCell(15).setCellValue(student.getF_score());
            dataRow.createCell(16).setCellValue(student.getG_score());
        }

        try {
            // 将工作簿写入文件
            FileOutputStream fileOut = new FileOutputStream(filePath);
            workbook.write(fileOut);
            fileOut.close();
            // 关闭工作簿
            workbook.close();
            b=true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return b;
    }
}